package com.sgb.kjwl.view.widget.lease;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.lease.DictEntity4;
import com.sgb.kjwl.model.entity.request.FactoryFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter3;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

/**
 * 工厂筛选界面
 */
public class FactoryFliterPopWindow extends BasePopWindow {
    private LinearLayout rootLayout;
    private TextView typeView;
    private TextView brandlView;
    private TextView moneyView;
    private TextView floorView;
    private TextView levelView;
    private TextView sortView;
    private TextView okView;
    private TextView resetView;
    private MaxRecyclerView typeRecyclerView;
    private MaxRecyclerView brandRecyclerView;
    private MaxRecyclerView moneyRecyclerView;
    private MaxRecyclerView floorRecyclerView;
    private MaxRecyclerView levelRecyclerView;
    private MaxRecyclerView sortRecyclerView;
    private MaterialFilterAdapter3 typeAdapter;
    private MaterialFilterAdapter3 brandAdapter;
    private MaterialFilterAdapter3 moneyAdapter;
    private MaterialFilterAdapter3 floorAdapter;
    private MaterialFilterAdapter3 levelAdapter;
    private MaterialFilterAdapter3 sortAdapter;
    private FilterResultClick filterResultClick;
    private FactoryFilterJson filterJson;
    private List<String> areaList;
    private List<String> materKeyList;
    private List<String> materValueList;
    private List<String> tempMaterKeyList;
    private List<String> tempMaterValueList;
    private List<DictEntity4> tempDictList;
    private List<MaterialFilterAdapter3> adapterList;
    private boolean isFir = true;

    public interface FilterResultClick {
        void result(FactoryFilterJson filterJson, List<DictEntity3> data, SparseBooleanArray checkBooleanArray);

        void queryTypeChild(String id);

        void queryNameChild(String id);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public FactoryFliterPopWindow(Context context) {
        super(context, DIR_RIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_factory_fliter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);

        typeView = holder.getPop().findViewById(R.id.type_view);
        brandlView = holder.getPop().findViewById(R.id.brand_view);
        moneyView = holder.getPop().findViewById(R.id.money_view);
        floorView = holder.getPop().findViewById(R.id.floor_view);
        levelView = holder.getPop().findViewById(R.id.level_view);
        sortView = holder.getPop().findViewById(R.id.sort_view);

        typeRecyclerView = holder.getPop().findViewById(R.id.type_list);
        brandRecyclerView = holder.getPop().findViewById(R.id.brand_list);
        moneyRecyclerView = holder.getPop().findViewById(R.id.money_list);
        floorRecyclerView = holder.getPop().findViewById(R.id.floor_list);
        levelRecyclerView = holder.getPop().findViewById(R.id.level_list);
        sortRecyclerView = holder.getPop().findViewById(R.id.sort_list);

        typeRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        brandRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        moneyRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        floorRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        levelRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        sortRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));

        typeAdapter = new MaterialFilterAdapter3(mContext, null);
        typeAdapter.setItemSelect(true);
        typeAdapter.setDefItem(-1);
        typeAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                if (filterResultClick != null) {
                    filterResultClick.queryTypeChild(materialFilterEntity.getId());
                }
                filterJson.setFirstCategoryNo(materialFilterEntity.getId());
            }
        });
        typeRecyclerView.setAdapter(typeAdapter);

        brandAdapter = new MaterialFilterAdapter3(mContext, null);
        brandAdapter.setItemSelect(true);
        brandAdapter.setDefItem(-1);
        brandAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                filterJson.setSecondCategoryNo(materialFilterEntity.getId());
                filterJson.setSecondCategoryName(materialFilterEntity.getName());
            }
        });
        brandRecyclerView.setAdapter(brandAdapter);

        moneyAdapter = new MaterialFilterAdapter3(mContext, null);
        moneyAdapter.setItemSelect(true);
        moneyAdapter.setDefItem(-1);
        moneyAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                filterJson.setRentalArea(materialFilterEntity.getId());
                filterJson.setRentalAreaName(materialFilterEntity.getName());
            }
        });
        moneyRecyclerView.setAdapter(moneyAdapter);

        floorAdapter = new MaterialFilterAdapter3(mContext, null);
        floorAdapter.setItemSelect(true);
        floorAdapter.setDefItem(-1);
        floorAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                filterJson.setFloorType(materialFilterEntity.getId());
            }
        });
        floorRecyclerView.setAdapter(floorAdapter);

        levelAdapter = new MaterialFilterAdapter3(mContext, null);
        levelAdapter.setItemSelect(true);
        levelAdapter.setDefItem(-1);
        levelAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                filterJson.setFireLevel(materialFilterEntity.getId());
            }
        });
        levelRecyclerView.setAdapter(levelAdapter);

        sortAdapter = new MaterialFilterAdapter3(mContext, null);
        sortAdapter.setItemSelect(true);
        sortAdapter.setDefItem(-1);
        sortAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                filterJson.setSortWay(materialFilterEntity.getId());
                filterJson.setSortName(materialFilterEntity.getName());
            }
        });
        sortRecyclerView.setAdapter(sortAdapter);

        filterJson = new FactoryFilterJson();
        areaList = new ArrayList<>();
        materKeyList = new ArrayList<>();
        materValueList = new ArrayList<>();
        tempMaterKeyList = new ArrayList<>();
        tempMaterValueList = new ArrayList<>();
        tempDictList = new ArrayList<>();
        adapterList = new ArrayList<>();

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (filterResultClick != null) {
                    filterResultClick.result(filterJson, null, null);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetData();
            }
        });
    }

    private void resetData() {
        for (MaterialFilterAdapter3 adapter : adapterList) {
            adapter.setDefItem(-1);
        }
        for (int i = 0; i < materValueList.size(); i++) {
            materValueList.set(i, "");
        }

//        typeAdapter.setDefItem(0);
//        filterJson.setFirstCategoryNo(typeAdapter.getDatas().get(0).getId());

//        brandAdapter.setDefItem(0);
//        filterJson.setSecondCategoryNo(brandAdapter.getDatas().get(0).getId());
//        filterJson.setSecondCategoryName(brandAdapter.getDatas().get(0).getName());

        moneyAdapter.setDefItem(0);
        filterJson.setRentalArea(moneyAdapter.getDatas().get(0).getId());
        filterJson.setRentalAreaName(moneyAdapter.getDatas().get(0).getName());

        floorAdapter.setDefItem(0);
        filterJson.setFloorType(floorAdapter.getDatas().get(0).getId());

        levelAdapter.setDefItem(0);
        filterJson.setFireLevel(levelAdapter.getDatas().get(0).getId());

        sortAdapter.setDefItem(0);
        filterJson.setSortWay(sortAdapter.getDatas().get(0).getId());
        filterJson.setSortName(sortAdapter.getDatas().get(0).getName());
        areaList.clear();
    }

    public void setTypeData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            typeView.setVisibility(View.GONE);
        } else {
//            typeView.setVisibility(View.VISIBLE);
        }
        if (isFir) {
            typeAdapter.setDefItem(0);
            filterJson.setFirstCategoryNo(data.get(0).getId());
            isFir = false;
        }
        typeAdapter.setDatas(data);
    }

    public void setTypeDefault(int position, String id) {
        typeAdapter.setDefItem(position);
        filterJson.setFirstCategoryNo(id);
    }

    public void setBrandData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            brandlView.setVisibility(View.GONE);
        } else {//类型废弃不展示
//            brandlView.setVisibility(View.VISIBLE);
        }
        if (data == null || data.size() == 0) {
            return;
        }
        brandAdapter.setDefItem(0);
        filterJson.setSecondCategoryNo(data.get(0).getId());
        brandAdapter.setDatas(data);
    }

    public void setBrandDefault(int position, String id) {
        brandAdapter.setDefItem(position);
        filterJson.setSecondCategoryNo(id);
    }

    public void setMoneyData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            moneyView.setVisibility(View.GONE);
        } else {
            moneyView.setVisibility(View.VISIBLE);
        }
        moneyAdapter.setDatas(data);
    }

    public void setMoneyDefault(int position, String id) {
        moneyAdapter.setDefItem(position);
        filterJson.setRentalArea(id);
    }

    public void setFloorData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            floorView.setVisibility(View.GONE);
        } else {
            floorView.setVisibility(View.VISIBLE);
        }
        floorAdapter.setDatas(data);
    }

    public void setFloorDefault(int position, String id) {
        floorAdapter.setDefItem(position);
        filterJson.setFloorType(id);
    }

    public void setFireLevelData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            levelView.setVisibility(View.GONE);
        } else {
            levelView.setVisibility(View.VISIBLE);
        }
        levelAdapter.setDatas(data);
    }

    public void setFireLevelDefault(int position, String id) {
        levelAdapter.setDefItem(position);
        filterJson.setFireLevel(id);
    }

    public void setSortData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            sortView.setVisibility(View.GONE);
        } else {
            sortView.setVisibility(View.VISIBLE);
        }
        sortAdapter.setDatas(data);
    }

    public void setSortDefault(int position, String id) {
        sortAdapter.setDefItem(position);
        filterJson.setSortWay(id);
    }

    public void setSpecData(List<DictEntity3> data) {
        adapterList.clear();
        rootLayout.removeAllViews();
        materKeyList.clear();
        materValueList.clear();
        for (int i = 0; i < data.size(); i++) {
            materKeyList.add(data.get(i).getId());
            materValueList.add("");
            addTitleView(data.get(i).getName());
            addRecyclerView(data.get(i).getDatas(), i);
        }
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextSize(12);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_999999));
        textView.setPadding(0, mContext.getResources().getDimensionPixelOffset(R.dimen.d10), 0, mContext.getResources().getDimensionPixelOffset(R.dimen.d10));
        rootLayout.addView(textView, layoutParams);
    }

    private void addRecyclerView(List<DictEntity3> data, final int index) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        MaterialFilterAdapter3 childAdapter = new MaterialFilterAdapter3(mContext, null);
        childAdapter.setItemSelect(true);
        childAdapter.setDefItem(-1);
        childAdapter.setIndex(index);
        adapterList.add(childAdapter);
        MaxRecyclerView recyclerView = new MaxRecyclerView(mContext);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(childAdapter);
        childAdapter.setDatas(data);
        childAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materValueList.set(childAdapter.getIndex(), materialFilterEntity.getId());
            }
        });
        rootLayout.addView(recyclerView, layoutParams);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}
